﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
public partial class AdminPortal_Controls_Tintuc_ListCategoryNews : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    TblDanhMucTinManager _TblDanhMucTinManager = new TblDanhMucTinManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //LoadToGridvew();
            TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
            _TblDanhMucTinEntity.LangId = LangID;
            _TblDanhMucTinEntity.SortDirection = "ASC";
            _TblDanhMucTinEntity.Type = "category";
            LoadDataToGrid(_TblDanhMucTinEntity);

            //--load danh muc cha
            LoadDanhMucCha();
        }
    }

    //private void LoadToGridvew()
    //{
    //    TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
    //    _TblDanhMucTinEntity.LangId = LangID;

    //    DataTable _dt = _TblDanhMucTinManager.be_SelectToGrivew(_TblDanhMucTinEntity);

    //    if (_dt != null)
    //    {
    //        lblTotalRecord.Text = _dt.Rows.Count.ToString();
    //        dgvListCategoryNews.DataSource = _dt;
    //        dgvListCategoryNews.DataBind();
    //    }
    //}

    private void LoadDanhMucCha()
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        _TblDanhMucTinEntity.LangId = LangID;
        _TblDanhMucTinEntity.SortDirection = "ASC";
        _TblDanhMucTinEntity.Type = "category";
        DataTable _dtCha = _TblDanhMucTinManager.be_SelectToDropdownlist(_TblDanhMucTinEntity);

        if (_dtCha != null)
        {
            HNG.LIB.BIND_SOURCE_V3.Bind_DropDownList(ddlDanhMucCha, _dtCha, TblDanhMucTinFields.TenDanhMuc.Name, TblDanhMucTinFields.Id.Name,TblDanhMucTinFields.DanhMucCha.Name, true);
        }
    }

    private void LoadDataToGrid(TblDanhMucTinEntity _TblDanhMucTinEntity)
    {
        DataTable dt = _TblDanhMucTinManager.be_SelectToDataList(_TblDanhMucTinEntity);
        if (dt != null)
        {
            lblTotalRecord.Text = dt.Rows.Count.ToString();
            dgvListCategoryNews.DataSource = dt;
            dgvListCategoryNews.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/CategoryNewsAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvListCategoryNews.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    _TblDanhMucTinManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        LoadDataToGrid(new TblDanhMucTinEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        _TblDanhMucTinEntity.SortExpression = TblDanhMucTinFields.ThuTu.Name;
        _TblDanhMucTinEntity.SortDirection = "ASC";
        _TblDanhMucTinEntity.Condition = txtKeySearch.Text;
        _TblDanhMucTinEntity.LangId = LangID;
        _TblDanhMucTinEntity.Type = "category";

        LoadDataToGrid(_TblDanhMucTinEntity);

    }
    protected void dgvListCategoryNews_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkCategoryName = (HyperLink)e.Row.FindControl("lnkCategoryName");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        Image imgIcon = (Image)e.Row.FindControl("imgIcon");

        if (r != null && lnkCategoryName != null && imgUpdate != null)
        {
            lnkCategoryName.Text = r[TblDanhMucTinFields.TenDanhMuc.Name].ToString();
            lnkCategoryName.NavigateUrl = "~/AdminPortal/Pages/CategoryNewsEdit.aspx?id=" + r[TblDanhMucTinFields.Id.Name].ToString();

            if (r[TblDanhMucTinFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatuscateNews('" + imgUpdate.ClientID + "','" + r[TblDanhMucTinFields.Id.Name].ToString() + "')");

            if (!string.IsNullOrEmpty(r[TblDanhMucTinFields.Icon.Name].ToString()))
                imgIcon.ImageUrl = r[TblDanhMucTinFields.Icon.Name].ToString();
            else
                imgIcon.Visible = false;
        }
    }
    protected void ddlDanhMucCha_SelectedIndexChanged(object sender, EventArgs e)
    {
        TblDanhMucTinEntity _TblDanhMucTinEntity = new TblDanhMucTinEntity();
        _TblDanhMucTinEntity.SortExpression = TblDanhMucTinFields.ThuTu.Name;
        _TblDanhMucTinEntity.SortDirection = "ASC";
        _TblDanhMucTinEntity.Type = "category";
        string txt = ddlDanhMucCha.SelectedItem.Text;
        txt = txt.Replace("--", "");
        _TblDanhMucTinEntity.Condition = txt;
        _TblDanhMucTinEntity.LangId = LangID;

        LoadDataToGrid(_TblDanhMucTinEntity);
    }
}